package design_pattern.single;

/**
 * @Package: design_pattern.single
 * @ClassName: SingleTon02
 * @Author: x2606
 * @CreateTime: 2022/3/10 21:32
 * @Description: 单例模式-懒汉式
 */

//懒汉式
//在静态方法中实例化对象，如果不去调用，就不会实例化，避免资源浪费。
public class SingleTon02 {
}

//只能养一只猫
class Cat{
    private String name;

    private static Cat cat;

    private Cat(String name) {
        this.name = name;
    }

    public static Cat getInstance() {
        //调用才初始化
        if (cat==null){
            cat =new Cat("小黄");
        }
        return cat;
    }
}
